<script setup>
import { useRouter } from 'vue-router'
import { useCartStore } from '@/stores/cart'

const cartStore = useCartStore()

const router = useRouter()
//购物车

//点击购物车列表里面的商品进入商品详情
const handleCart = (id) => {
  router.push(`/detail/${id}`)
}
//购物车结算
const handleBuy = () => {
  router.push('/cart')
}
//删除
const handledel = (item) => {
  console.log(item)
  cartStore.delCartList(item.skuId)
}
</script>
<template>
  <!-- 购物车 -->
  <div class="carts">
    <el-popover trigger="click" :width="320">
      <template #reference>
        <el-badge :value="cartStore.totalNum">
          <el-link
            icon="ShoppingCart"
            class="cart"
            :underline="false"
          ></el-link>
        </el-badge>
      </template>
      <!-- 购物车商品列表 -->
      <el-empty
        desciption="购物车为空"
        v-if="cartStore.cartList?.length <= 0"
      ></el-empty>

      <div class="cards" v-else>
        <el-card
          class="card"
          shadow="hover"
          v-for="item in cartStore.cartList"
          :key="item.id"
          @click="handleCart(item.id)"
        >
          <div
            style="
              display: flex;
              align-items: center;
              justify-content: space-between;
            "
          >
            <div style="display: flex">
              <el-image :fit="cover" :src="item.picture" />
              <div>
                <p
                  style="
                    margin-top: 0px;
                    margin-bottom: 0px;
                    font-size: small;
                    font-family: '像素字体';
                  "
                >
                  {{ item.name }}
                </p>
                <div
                  style="
                    margin-top: 0px;
                    margin-bottom: 0px;
                    overflow: hidden;
                    display: -webkit-box;
                    -webkit-box-orient: vertical;
                    -webkit-line-clamp: 1;
                    font-family: '像素字体';
                    font-size: smaller;
                  "
                >
                  <span style="margin-right: 10px">
                    {{ item.attrsText }}
                  </span>
                </div>
              </div>
            </div>
            <a href="javascript:;" @click="handledel(item)">
              <el-icon size="large" color="black" class="close"
                ><Close /></el-icon
            ></a>
          </div>
        </el-card>
      </div>

      <div
        class="floor"
        style="
          display: flex;
          align-items: center;
          justify-content: space-between;
        "
      >
        <div>
          <p style="margin-top: 20px; font-family: '小可奶酪体'">
            共{{ cartStore.totalNum }}件商品
          </p>
          <p
            style="
              font-size: large;
              color: coral;
              margin-top: 0px;
              font-family: '小可奶酪体';
            "
          >
            ￥{{ cartStore.totalPrice }}
          </p>
        </div>
        <el-button
          type="primary"
          @click="handleBuy"
          style="font-family: '小可奶酪体'"
          >去购物车结算</el-button
        >
      </div>
    </el-popover>
  </div>
</template>
<style scoped lang="scss">
.cart {
  font-size: 30px;
  margin-left: 25px;
  margin-bottom: 10px;
}
.cards {
  display: flex;
  overflow: auto;
  height: 30vh;
  flex-wrap: wrap;
  .card {
    width: 100%;
    height: 60px;
    padding: 4px;
    .close {
      opacity: 0;
    }
    &:hover {
      .close {
        opacity: 1;
      }
    }
  }
  :deep(.el-image) {
    width: 50px;
    height: 50px;
    overflow: visible;
  }
}
.el-card {
  --el-card-padding: 0px;
}
</style>
